package gson.emlog.adapter;

import gson.emlog.R;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CloudTwittersAdapter extends BaseAdapter {
	private JSONArray items;
	private Context mContext;

	public CloudTwittersAdapter(JSONArray data, Context mContext) {
		super();
		this.items = data;
		this.mContext = mContext;
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		try {
			return items.get(position);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		// 获取layout实例对象
		LayoutInflater lif = LayoutInflater.from(mContext);
		LinearLayout item = (LinearLayout) lif.inflate(
				R.layout.cloud_twitters_item, null);

		try {
			if (item != null) {
				JSONObject data = (JSONObject) items.get(position);
				// 赋值
				TextView pepole = (TextView) item
						.findViewById(R.id.clound_tw_from);
				pepole.setText(data.get("blogname").toString());

				TextView content = (TextView) item
						.findViewById(R.id.clound_tw_content);
				content.setText(data.get("content").toString());

				TextView date = (TextView) item
						.findViewById(R.id.clound_tw_date);
				date.setText(data.get("date").toString());

				return item;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}
}
